#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<unordered_map>

const std::string sep=":";
const std::string default_path="./dictionary";
class Dict
{
    public:
    Dict(const std::string& confpath=default_path):_confpath(confpath){LoadDict();}
    ~Dict(){}
    void LoadDict()
    {
        std::ifstream in(_confpath);
        if(!in.is_open())
        {
            std::cerr<<"open file error:"<<std::endl;
            return;
        }
        std::string line;
        while(getline(in,line))
        {
            if(line.empty())
                continue;
            auto pos=line.find(sep);
            if(pos==std::string::npos)
                continue;
            std::string key=line.substr(0,pos);
            std::string val=line.substr(pos+sep.size());
            _dict[key]=val;
        }
        in.close();
    }
    std::string Translate(const std::string word)
    {
        auto iter=_dict.find(word);
        if(iter==_dict.end())
            return "UnKnow";
        return iter->second;
    }
    private:
    std::string _confpath;
    std::unordered_map<std::string,std::string> _dict;
};